<template>
  <div>
    <div class="banner">
      <div class="imgs">
        <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
          <van-swipe-item v-for="(item, index) in imgs" :key="index">
            <img :src="item.pic" alt="" />
          </van-swipe-item>
        </van-swipe>
      </div>
    </div>
    <div class="remLists">
      <div class="title">推荐歌单</div>
      <div class="content">
        <ul>
          
          <li v-for="(item, index) in personalized" :key="index" >
            <router-link :to="{path:'/recommendlist',query:{id:item.id}}">
            <div class="img">
              <img :src="item.picUrl" alt="" />
            </div>
            <span
              ><i class="iconfont icon-erji"></i
              >{{ item.playCount | playCountNum }}</span
            >
            <p>{{ item.name }}</p>
            </router-link>
          </li>
          
        </ul>
      </div>
    </div>
    <div class="dj">
      <div class="title">推荐电台</div>
      <div class="content" ref="dj">
        <ul ref="cont">
          <li v-for="(item, index) in djList" :key="index">
            <div class="img">
              <img :src="item.picUrl" alt="" ref="djimg" />
            </div>
            <p>{{ item.name }}</p>
          </li>
        </ul>
      </div>
    </div>
    <div class="dj topsinger">
      <div class="title">热门歌手</div>
      <div class="content" ref="topsinger">
        <ul ref="cont2">
          <li v-for="(item, index) in toparList" :key="index">
            <router-link
                 :to="{path:'/singerlist',query:{id:item.id,picUrl:item.picUrl}}" 
                >
            <div class="singerimg" :style="{'background-image':'url('+item.picUrl+')'}">
              <!-- <img :src="item.picUrl" alt="" ref="djimg" /> -->
            </div>
            <p>{{ item.name }}</p>
            </router-link>
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>
<script>
import { getBanner, getPersonalized, getDjprogram,getTopArtists } from "../api/recommend";
import BScroll from "better-scroll";
export default {
  data() {
    return {
      imgs: null,
      personalized: null,
      djList: null,
      toparList:""
    };
  },
  methods: {
    getTopArtistsFun(){
      getTopArtists({
        limit:10
      }).then(data=>{
        console.log(data);
        this.toparList = data.artists;
      })
    },
    getBannerFun() {
      getBanner().then((data) => {
        if (data.code == 200) {
          this.imgs = data.banners;
        }
      });
    },
    getPersonalizedFun() {
      getPersonalized().then((data) => {
        if (data.code == 200) {
          // console.log(data);
          this.personalized = data.result;
        }
      });
    },
    getDj() {
      getDjprogram().then((data) => {
        if (data.code == 200) {
          // console.log(data);
          this.djList = data.result;
        }
      });
    },
    // _initScroll() {
    //   // console.log(this.$refs)
    //   this.djScroll = new BScroll(this.$refs.dj, {
    //     click: true,
    //     // startX:0,
    //     scrollX: true,
    //     // scrollY:false,
    //     // eventPassthrough:'horizontal',
    //     probeType: 3,
    //     // hasHorizontalScroll: true
    //   });
    //   console.log(this.djScroll);
    // },
    verScroll() {        
      let width = 6 * 120.6; // 动态计算出滚动区域的大小，前面已经说过了，产生滚动的原因是滚动区域宽度大于父盒子宽度
      let width2 = 10 * 120.6;
      this.$refs.cont.style.width = width + "px"; // 修改滚动区域的宽度
      this.$refs.cont2.style.width = width2 + "px"; 
      this.$nextTick(() => {
        if (!this.scroll) {
          this.scroll = new BScroll(this.$refs.dj, {
            startX: 0, // 配置的详细信息请参考better-scroll的官方文档，这里不再赘述
            click: true,
            scrollX: true,
            scrollY: false,
            eventPassthrough: "vertical",
          });
          this.scroll2 = new BScroll(this.$refs.topsinger, {
            startX: 0, // 配置的详细信息请参考better-scroll的官方文档，这里不再赘述
            click: true,
            scrollX: true,
            scrollY: false,
            eventPassthrough: "vertical",
          });
        } else {
          this.scroll.refresh(); //如果dom结构发生改变调用该方法
        }
      });
    },
  },
  
  created() {
    this.getBannerFun();
    this.getPersonalizedFun();
    this.getDj();
    this.getTopArtistsFun();
  },
  mounted() {
    this.$nextTick(() => {
    //   this._initScroll();
    this.verScroll();
    });
  },
  filters: {
    playCountNum(value) {
      let w = value > 10000 ? value / 10000 : value; //万计算
      let y = w > 10000 ? parseInt(w / 10000) + "亿" : parseInt(w) + "万"; //亿计算
      return y;
    },
  },
};
</script>
<style lang="less">
.topsinger{
  // margin-top: 180px;
  li{
    margin-right: 20px !important;
    p{
      text-align: center;
    }
  }
  .singerimg{
    height: 120px;
    width: 100%;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: center center;
    border-radius: 50%;      
  }
}
.my-swipe .van-swipe-item {
  color: #fff;
  font-size: 20px;
  line-height: 150px;
  text-align: center;
  background-color: #39a9ed;
}
.banner {
  // height: 134px;
  // background-color: #d4473c;
  width: 100%;
  text-align: center;
  position: relative;
  overflow:hidden; 
  zoom:1;
  // margin-bottom: 12px;
  &::after{
    background-color: #d4473c;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 132px;
    content: "";
    display: block;
    z-index: -1;
  }
  .imgs {
    //   position: absolute;
    // left: 0px;
    // top: 0px;
    margin: 0 auto;
    min-height: 100px;
    width: 96%;
    border-radius: 5px;
    .my-swipe {
      min-height: 100px;
      img {
        width: 100%;
        border-radius: 5px;
      }
    }
  }
}
.remLists {
  .title {
    height: 60px;
    line-height: 60px;
    text-indent: 5px;
    font-size: 16px;
    color: #000;
    font-weight: bold;
  }
  .content {
    padding: 0 5px;
    ul {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      li {
        flex: 0 0 32%;
        // margin: 0 5px;
        position: relative;
        span {
          position: absolute;
          top: 0px;
          right: 0px;
          color: #fff;
          font-size: 10px;
          i {
            font-size: 15px;
            vertical-align: middle;
          }
        }
        .img {
          background-color: black;
          min-height: 50px;
          width: 100%;
          border-radius: 3px;
          img {
            width: 100%;
            border-radius: 3px;
          }
        }
        p {
          font-size: 12px;
          color: #565658;
          line-height: 15px;
          padding: 5px;
          margin-bottom: 10px;
        }
      }
    }
  }
}
.dj {
  .title {
    height: 60px;
    line-height: 60px;
    text-indent: 5px;
    font-size: 16px;
    color: #000;
    font-weight: bold;
  }
  .content {
    padding: 0 10px;
    overflow: hidden;
    // position: absolute;
    box-sizing: border-box;
    // touch-action: none;

    width: 100%;
    ul {
      width: 100%;
      display: flex;
      justify-content: space-between;
      li {
        flex: 0 0 120.06px;
        // margin: 0 5px;
        position: relative;
        margin-right: 5px;
        span {
          position: absolute;
          top: 0px;
          right: 0px;
          color: #fff;
          font-size: 10px;
          i {
            font-size: 15px;
            vertical-align: middle;
          }
        }
        .img {
          background-color: black;
          min-height: 50px;
          width: 100%;
          border-radius: 3px;
          img {
            width: 100%;
            border-radius: 3px;
          }
        }
        p {
          font-size: 12px;
          color: #565658;
          line-height: 15px;
          padding: 5px;
          margin-bottom: 10px;
        }
      }
    }
  }
}
</style>